
let dataSource = [
  {
    sysUserId: '1',
    sysUserHead: '',
    staffName: 'zhangsan',
    deptName: '技术部',
    staffType:'本部',
    staffJobName:'招聘专员',
    phoneNum:'123343433',
    isForbidden:'true',
    createTime:'2002-01-01',
    userCode:'zhangsan',
    password:'123456'
  },
  {
    sysUserId: '2',
    sysUserHead: '',
    staffName: '李四',
    deptName: '运营部',
    staffType:'本部',
    staffJobName:'运营专员',
    phoneNum:'12333445',
    isForbidden:'',
    createTime:'2024-01-01',
    userCode:'lisi',
    password:'123456'
    }

];

// 查询用户列表
const getUserList = (req,res) => {
  const { sysUserHead,staffName, deptName,staffType,staffJobName,phoneNum, isForbidden,createTime} = req.query;
  console.log(req.query)
  let filteredData = [...dataSource];
  if (staffName) {
    filteredData = filteredData.filter(item => item.staffName.includes(staffName));
  }
  let result = {}
  if (!staffName) {
    result = {
      data: dataSource,
      total: dataSource.length,
    }
    return res.send({ error_code: 0, error_msg: 'OK', data: result });
  } else {
    result = {
      data: filteredData,
      total: filteredData.length,
    }
    return res.send({ error_code: 0, error_msg: 'OK', data: result });
  }
}


//新增/编辑用户数据
const addUser = (req,res) => {
  //获取请求体中的入参
  console.log(req)
  req.body.forEach(element => {
    const { sysUserId,sysUserHead,staffName, deptName,staffType,staffJobName,phoneNum, isForbidden,createTime} = element;
    let newKey;
    if (sysUserId) {
        newKey = sysUserId;
    } else {
        newKey = moment().valueOf().toString();
    }

    const existingData = dataSource.find(item => item.sysUserId === newKey);
    if (existingData) {
        // 如果存在，则覆盖原有数据
        existingData.sysUserHead = sysUserHead
        existingData.staffName = staffName;
        existingData.deptName = deptName;
        existingData.staffType = staffType;
        existingData.staffJobName = staffJobName;
        existingData.phoneNum = phoneNum;
        existingData.isForbidden = isForbidden;
        existingData.createTime = createTime;
    } else {
        // 如果不存在，则添加新数据
        dataSource.push({
          sysUserId: newKey,
          sysUserHead,
          staffName, 
          deptName,
          staffType,
          staffJobName,
          phoneNum, 
          isForbidden,
          createTime
        });
    }
  });
  res.send({ error_code: 0, error_msg: 'OK', data: dataSource });
  
}

export default {
'GET /api/UserManagement/getUserList': getUserList,
'POST /api/UserManagement/addUser': addUser,
};